package com.woniuxy.productservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.status.CoachReservState;
import com.woniuxy.core.status.ProductState;
import com.woniuxy.core.status.UserState;
import com.woniuxy.productclient.client.UserFeignClient;
import com.woniuxy.productclient.client.dto.UserInfoDto;
import com.woniuxy.productdao.mapper.CoachReservMapper;
import com.woniuxy.productdao.model.AloneCourse;
import com.woniuxy.productdao.model.CoachReserv;
import com.woniuxy.productmodel.dto.CoachReservDto;
import com.woniuxy.productmodel.dto.CoachReservInfoDto;
import com.woniuxy.productservice.exception.ProductException;
import com.woniuxy.productservice.service.AloneCourseService;
import com.woniuxy.productservice.service.CoachReservService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class CoachReservServiceImpl extends ServiceImpl<CoachReservMapper, CoachReserv> implements CoachReservService {
    @Autowired
    private AloneCourseService aloneCourseService;
    @Autowired
    private UserFeignClient ufc;

    /**
     * 用户发起私教预约
     * @param productId
     * @param userId
     * @return
     * 用户端发起私教预约申请，教练根据自身情况决定是否接受，接受的话，学习细节电话联系，并生成订单，否则可拒绝申请。
     */
    @Override
    public CoachReservDto addCoachReserv(Integer productId, Integer userId) {
        AloneCourse aloneCourse = aloneCourseService.getById(productId);
        if (ObjectUtils.isEmpty(aloneCourse)) throw new ProductException(60001,"该私教课程不存在");
        //查询用户的信息，判断用户的状态及认证状态
        Result<UserInfoDto> res1 = ufc.findUserById(userId);
        if (!res1.getFlag()) throw new ProductException(res1.getCode(),res1.getMsg());
        UserInfoDto userInfo = res1.getData();
        if (ObjectUtils.isEmpty(userInfo)) throw new ProductException(60004,"用户不存在");
        if (userInfo.getUserState()!= UserState.IS_ENABLE.getState()) throw new ProductException(60005,"用户未启用");
        if (userInfo.getAuthState()!=UserState.IS_AUTH.getState()) throw new ProductException(60006,"用户未实名认证");
        if (aloneCourse.getProductState()!= ProductState.IS_ENABLE.getState()) throw new ProductException(60002,"私教产品未上架");
        boolean flag = new LambdaQueryChainWrapper<CoachReserv>(getBaseMapper())
                .eq(CoachReserv::getUserId, userId)
                .eq(CoachReserv::getCoachProductId, productId)
                .eq(CoachReserv::getReservState, CoachReservState.RESERVING.getName()).exists();
        if (flag) throw new ProductException(60003,"用户发起的该私教课程正在预约中");
        CoachReserv coachReserv = new CoachReserv();
        coachReserv.setCoachProductId(aloneCourse.getCoachProductId());
        coachReserv.setCoachId(aloneCourse.getCoachId());
        coachReserv.setUserId(userId);
        coachReserv.setUserName(userInfo.getUserName());
        coachReserv.setUserPhone(userInfo.getUserPhone());
        coachReserv.setReservState(CoachReservState.RESERVING.getName());
        save(coachReserv);
        CoachReservDto dto = BeanUtil.toBean(coachReserv, CoachReservDto.class);
        dto.setCoachRemark(aloneCourse.getCoachRemark());
        dto.setCoachName(aloneCourse.getCoachName());
        return dto;
    }

    /**
     * 用户端取消私教预约
     * @param coachReservId
     * @param userId
     */
    @Override
    public void cancelCoachReserv(Integer coachReservId, Integer userId) {
        //用户只能取消预约中状态的申请，只能取消自己的预约
        CoachReserv coachReserv = getById(coachReservId);
        if (ObjectUtils.isEmpty(coachReserv)) throw new ProductException(60006,"预约不存在");
        if (coachReserv.getUserId()!=userId) throw new ProductException(60007,"不是该用户的预约");
        if (!coachReserv.getReservState().equals(CoachReservState.RESERVING.getName()))
            throw new ProductException(60008,"不是预约中的状态");
        coachReserv.setReservState(CoachReservState.CANCEL.getName());
        updateById(coachReserv);
    }

    /**
     * 用户端查看私教预约
     * @param userId
     * @return
     */
    @Override
    public List<CoachReservDto> findCoachReservByUser(Integer userId) {
        List<CoachReserv> coachReservs = list(new LambdaQueryWrapper<CoachReserv>()
                .eq(CoachReserv::getUserId, userId));
        List<CoachReservDto> dtos = BeanUtil.copyToList(coachReservs, CoachReservDto.class);
        for (CoachReservDto dto : dtos) {
            AloneCourse aloneCourse = aloneCourseService.getById(dto.getCoachProductId());
            dto.setCoachName(aloneCourse.getCoachName());
            dto.setCoachRemark(aloneCourse.getCoachRemark());
        }
        return dtos;
    }

    /**
     * 教练端查看私教预约成功
     * @param coachId
     * @return
     */
    @Override
    public List<CoachReservDto> findCoachReservByCoach(Integer coachId) {
        List<CoachReserv> coachReservs = list(new LambdaQueryWrapper<CoachReserv>()
                .eq(CoachReserv::getCoachId, coachId));
        List<CoachReservDto> dtos = BeanUtil.copyToList(coachReservs, CoachReservDto.class);
        for (CoachReservDto dto : dtos) {
            AloneCourse aloneCourse = aloneCourseService.getById(dto.getCoachProductId());
            dto.setCoachName(aloneCourse.getCoachName());
            dto.setCoachRemark(aloneCourse.getCoachRemark());
        }
        return dtos;
    }

    /**
     * 教练端拒绝私教预约
     * @param coachReservId 预约Id
     * @param coachId 教练id
     */
    @Override
    public void refuseCoachReserv(String message,Integer coachReservId, Integer coachId) {
        //教练只能拒绝预约中的邀约
        CoachReserv coachReserv = getById(coachReservId);
        if (ObjectUtils.isEmpty(coachReserv)) throw new ProductException(60001,"预约不存在");
        if (coachReserv.getCoachId()!=coachId) throw new ProductException(60002,"不是该教练的预约");
        if (!coachReserv.getReservState().equals(CoachReservState.RESERVING.getName()))
            throw new ProductException(60002,"只能拒绝预约中状态的邀约");
        coachReserv.setReservState(CoachReservState.REFUSE.getName());
        coachReserv.setMessage(message);
        updateById(coachReserv);
    }


    /**
     * 教练端同意私教邀约
     * @param coachReservId
     * @param coachId
     */

    @Override
    public void agreeCoachReserv(Integer coachReservId, Integer coachId) {
        //教练同意邀约后，教练生成私教产品的订单，待用户支付后生成进度
        CoachReserv coachReserv = getById(coachReservId);
        if (ObjectUtils.isEmpty(coachReserv)) throw new ProductException(60001,"预约不存在");
        if (coachReserv.getCoachId()!=coachId) throw new ProductException(60002,"不是该教练的预约");
        if (!coachReserv.getReservState().equals(CoachReservState.RESERVING.getName()))
            throw new ProductException(60002,"只能同意预约中状态的邀约");
        coachReserv.setReservState(CoachReservState.AGREE.getName());
        updateById(coachReserv);
        //教练去订单服务给用户下单
    }

    /**
     * 教练端根据id查找私教预约
     * @param coachReservId
     * @return
     */
    @Override
    public CoachReservInfoDto findCoachReservById(Integer coachReservId) {
        CoachReserv coachReserv = getById(coachReservId);
        if (null == coachReserv) return null;
        CoachReservInfoDto dto = BeanUtil.toBean(coachReserv, CoachReservInfoDto.class);
        AloneCourse aloneCourse = aloneCourseService.getById(dto.getCoachProductId());
        dto.setCoachProductPrice(aloneCourse.getCoachProductPrice());
        dto.setCoachRemark(aloneCourse.getCoachRemark());
        return dto;
    }


}
